﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Xml;


namespace WpfApplication1
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openDlg = new Microsoft.Win32.OpenFileDialog();
            openDlg.Filter = "Excel 工作薄(*.xls, *.xlsx)|*.xls;*.xlsx";
            if (openDlg.ShowDialog() == true)
            {
                string strFileName = openDlg.FileName;
                Excel fileExcel = new Excel();
                fileExcel.Open(strFileName);
                ArrayList listQeus = fileExcel.QuestionAnswer;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
                XmlNode rootNode = xmlDoc.CreateElement("QAList");
                for (int i = 0; i < listQeus.Count; i++)
                {
                    XmlNode subNode = xmlDoc.CreateElement("QA");

                    XmlAttribute subAtt = xmlDoc.CreateAttribute("Q");
                    QuestionMgr oneQA = listQeus[i] as QuestionMgr;
                    subAtt.Value = oneQA.Question;
                    subNode.Attributes.Append(subAtt);

                    subAtt = xmlDoc.CreateAttribute("A");
                    subAtt.Value = oneQA.Anwser;
                    subNode.Attributes.Append(subAtt);
                    rootNode.AppendChild(subNode);
                }
                xmlDoc.AppendChild(rootNode);

                string strCurDir = System.IO.Directory.GetCurrentDirectory();
                strCurDir = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                strCurDir += "test.xml";

                xmlDoc.Save(strCurDir);


                XmlDataProvider xdp = new XmlDataProvider();
                xdp.Document = xmlDoc;
                xdp.XPath = @"/QAList/QA";

                this.listQuestions.DataContext = xdp;
                this.listQuestions.SetBinding(ListView.ItemsSourceProperty, new Binding());
            }

            return;

            //Http.LoginBaidu(strUser, strPwd);
        }

        private void titleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }  
        }

        private void Button_Click_Close(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_Max(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
        }

        private void Button_Click_Min(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;

        }
    }
}
